package com.it.dao.impl;


import com.it.Bean.Guestbook;
import com.it.dao.GuestbookDao;
import com.it.db.DBManager;
import com.it.db.JdbcTemplate;
import com.it.mapping.EntityMapping;
import com.it.mapping.MappingFactory;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl  implements GuestbookDao {
    public static DBManager dbManager=null;
    public static Connection conn =null;
    public static JdbcTemplate jdbcTemplate=null;
    public static MappingFactory mappingFactory=null;
    public static EntityMapping map=null;
    static{
        //抽取数据库连接信息
        dbManager = DBManager.getInstance();
        //获得数据库连接
        conn = dbManager.getConnection();
        //获取JDBC工具类
        jdbcTemplate = JdbcTemplate.getInstance();
        //设置连接
        jdbcTemplate.setConnection(conn);
        //获取封装类工厂
        mappingFactory = MappingFactory.getInstance();
        //获取封装类
        map = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
    }
    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql = "select * from guestbook";

        List<Object> list = jdbcTemplate.query(sql,map);
        return list;
    }

    @Override
    public Integer insert(Guestbook gb) throws SQLException {
        String sql = "insert into guestbook(name,phone,email,title,content,createdtime) values(?,?,?,?,?,?) ";
        int update = jdbcTemplate.update(sql, gb.getName(), gb.getPhone(), gb.getEmail(),
                gb.getTitle(), gb.getContent(), gb.getCreatedtime());
        return update;
    }

    @Override
    public Integer delete(int id) throws SQLException {
        String sql = " delete from guestbook where id=? ";
        int update = jdbcTemplate.update(sql, id);

        return update;
    }
}
